﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PIM_ATE
{
    public class ClsDataRcuConfigBuilder : ClsDataConfigBase
    {
        public ClsDataRcuConfigBuilder(string alias)
            : base(alias)
        {
            CONFIG_TYPE = "RCU";
            CONDITION_SQL = " and [Type] = '" + CONFIG_TYPE + "';";
        }

        #region static function

        #region serialize

        public static ClsDataRcuConfig DeserializeFile(string xmlFilePath)
        {
            try
            {
                ClsDataRcuConfig SerializerObject;
                var xmlSerializer = new XmlSerializer(typeof(ClsDataRcuConfig));
                using (var sr = new StreamReader(xmlFilePath))
                {
                    SerializerObject = (ClsDataRcuConfig)xmlSerializer.Deserialize(sr);
                }

                return SerializerObject;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static ClsDataRcuConfig DeserializeString(string origin)
        {
            try
            {
                ClsDataRcuConfig SerializerObject;
                var xmlSerializer = new XmlSerializer(typeof(ClsDataRcuConfig));
                using (TextReader reader = new StringReader(origin))
                {
                    SerializerObject = (ClsDataRcuConfig)xmlSerializer.Deserialize(reader);
                }

                return SerializerObject;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static string SerializeString(ClsDataRcuConfig serializerObject)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(ClsDataRcuConfig));
                StringBuilder sb = new StringBuilder();
                XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true });
                xmlSerializer.Serialize(writer, serializerObject);
                writer.Close();

                return XmlUtil.FormatXml(sb.ToString());
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return "";
            }
        }

        public static bool SerializeFile(string sPath, ClsDataRcuConfig serializerObject)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(ClsDataRcuConfig));
                StringBuilder sb = new StringBuilder();
                StreamWriter writer = new StreamWriter(sPath);
                xmlSerializer.Serialize(writer, serializerObject);
                writer.Close();

                //XmlUtil.ToSoap(serializerObject, sPath);

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        #endregion

        #region 解析Config

        public static List<string> getConfigColNames()
        {
            List<string> names = new List<string>();
            names.Add("ID");
            return names;
        }

        public static List<string> getPIMColNames()
        {//显示头表
            List<string> names = new List<string>();
            names.Add("测试项目");
            return names;
        }

        #endregion

        public bool upload(string sProcode, string sVersion, ClsDataRcuConfig serializerObject, ref string error_info)
        {//上传更新配置
            string strXml = SerializeString(serializerObject);
            if (strXml == "")
            {
                return false;
            }

            return uploadXml(sProcode, sVersion, strXml, ref error_info);
        }

        public ClsDataRcuConfig download()
        {//下载当前版本的配置
            string sXml = downloadXml();
            if (sXml != null && sXml != "")
            {
                return DeserializeString(sXml);
            }
            return null;
        }

        public override DataTable getProCodeTable()
        {
            var lstConfig = download();
            if (lstConfig == null)
            {
                return null;
            }
            return lstConfig.toTable();
        }

        #endregion
    }
}
